package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

	@Override
	public List<Product> selectAll() {
		 
		return productMapper.selectAll();
	}

	@Override
	public Result selectByPage(Integer page, Integer limit, Product product) {
		System.out.println("ProductServiceImpl.selectByPage()");
		// 查询一种有多少条数据
		int count = productMapper.getTotalCount(product);
		// 当前页数据
		int offset = (page -1 ) * limit;
		List<Product> list = productMapper.selectByPage(offset, limit, product);
		System.out.println(product);
		
		return Result.success("", list, count);
	}

	@Override
	public List<Product> selectProductByCategoryId(Integer categoryId, Integer page, Integer limit) {
		//查询这个分类下一共有多少条数据
		int count = productMapper.getTotalCountByCategoryId(categoryId);
		System.out.println("count = " + count);
		int totalPage = (int)Math.ceil((double)count / (double)limit);
		System.out.println("totalPage = " + totalPage);
		//当前页数据
		int offset = (page - 1 ) * limit;
		List<Product> list = productMapper.selectProductByCategoryId(categoryId,offset,limit);
		return list;
	}

	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;
		for (Integer id : ids) {
			// 每删除一个影响一行c返回就是1
			int c = productMapper.deleteByPrimaryKey(id);
			count += c;
		}
		
		//int count = productMapper.deleteAll(ids);
		return count == ids.length ? true : false;
	}

	@Override
	public boolean insert(Product product) {
		int count = productMapper.insert(product);
		return count == 1 ? true : false;
	}

	@Override
	public Product selectProductById(int id) {
		Product product = productMapper.selectProductById(id);
		return product;
	}

	@Override
	public List<Product> selectProductByMaybeYLike(Integer categoryId , int id) {
		List<Product> listMYL = productMapper.selectProductByMaybeYLike(categoryId,id);
		return listMYL;
	}

	@Override
	public List<Product> selectNew(Integer page, Integer limit) {
		//当前页数据
		int offset = (page - 1 ) * limit;
		List<Product> list = productMapper.selectNew(page,limit);
		return list;
	}

	@Override
	public List<Product> selectCheap(Integer page, Integer limit) {
		//当前页数据
		int offset = (page - 1 ) * limit;
		List<Product> list = productMapper.selectCheap(page,limit);
		return list;
	}

	@Override
	public List<Product> selectTop(Integer page, Integer limit,Integer categoryId) {
		//当前页数据
		int offset = (page - 1 ) * limit;
		List<Product> list = productMapper.selectTop(page,limit,categoryId);
		return list;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = productMapper.deleteByPrimaryKey(id);
		return count == 1 ? true : false;
	}

	@Override
	public boolean update(Product product) {
		int count = productMapper.updateByPrimaryKey(product);
		return count == 1 ? true : false;
	}

	@Override
	public List<Product> selectProductByLikeName(String name, Integer page, Integer limit) {
		//查询这个分类下一共有多少条数据
		int count = productMapper.getTotalCountByLikeName(name);
		System.out.println("count = " + count);
		System.out.println("limit = " + limit);
		int totalPage = (int)Math.ceil((double)count / (double)limit);
		System.out.println("totalPage = " + totalPage);
		//当前页数据
		int offset = (page - 1 ) * limit;
		System.out.println("offset = " + offset);
		List<Product> list = productMapper.selectProductByLikeName(name,offset,limit);
		for (Product product1 : list) {
			System.out.println(product1);
		}
		return list;
	}



}
